package com.cs.pk.ssh;

import java.io.IOException;
import java.net.UnknownHostException;

import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.configuration.ConfigurationException;
import com.sshtools.j2ssh.configuration.ConfigurationLoader;
import com.sshtools.j2ssh.configuration.SshConnectionProperties;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;

public class SshManager {
    
    private SshClient ssh = null;
    private  SshConnectionProperties properties = null;
   
    public SshManager(){
	try {
	    ConfigurationLoader.initialize(false);
	    ssh = new SshClient();
	    ssh.setKexTimeout(Integer.MAX_VALUE);
	    ssh.setSocketTimeout(Integer.MAX_VALUE);
	    ssh.setUseDefaultForwarding(true);
	    properties = new SshConnectionProperties();
	    properties.setForwardingAutoStartMode(true);
	    properties.setPrefPublicKey("ssh-rsa");
	    
	} catch (ConfigurationException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	
    }
    
    public void setHost(String host){
	properties.setHost(host);
    }
    
    public void setPort(int port){
	properties.setPort(port);
    }
    
    
    public void connect() throws UnknownHostException, IOException{
	ssh.connect(properties, new IgnoreHostKeyVerification());
    }
    
    public int authenticateClient(String user,String password) throws IOException{
	 PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
	 pwd.setUsername(user);
	 pwd.setPassword(password);
	 return ssh.authenticate(pwd);
    }
    

}
